package com.hotel.controller;

import com.hotel.pojo.User;
import com.hotel.service.UserService;
import com.hotel.vo.PageResult;
import com.hotel.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        String token = userService.login(user);
        if (token == null){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }

    /**
     * 业务说明:
     *  1./user/list
     *  2.请求类型:GET
     *  3.参数接收:后台使用PageResult对象接收
     *  4.返回值:SysResult<PageResult>
     */
    @GetMapping("/list")
    public SysResult getEmpList(PageResult pageResult){
        //业务查询总数,分页条数
        pageResult = userService.getEmpList(pageResult);
        return SysResult.success(pageResult);
    }

    /**
     * 管理员查询
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public SysResult findEmpById(@PathVariable Integer userId){
        User user=userService.findEmpById(userId);
        return  SysResult.success(user);
    }

    @DeleteMapping("/{userId}")
    public SysResult delete(@PathVariable Integer userId){
        userService.delete(userId);
        return SysResult.success();
    }

    /**
     * 管理员新增
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public SysResult addEmp(@RequestBody User user){
        userService.addEmp(user);
        return  SysResult.success();
    }

    /**
     * 管理员修改
     */
    @PutMapping("/updateUser")
    public SysResult updateEmp(@RequestBody User user){
        userService.updateEmp(user);
        return SysResult.success();
    }

    /**
     * 用户注册
     */

    @PostMapping("/signUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return  SysResult.success();
    }

    /**
     * 修改员工上班状态
     * @param user
     * @return
     */
    @PutMapping("/status/{userId}/{status}")
    public SysResult updateStatus(User user){
        userService.updateStatus(user);
        return SysResult.success();
    }

}
